package org.theqcp.wow.sim.gui.tabs;

import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.net.URL;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;

import org.theqcp.wow.sim.gui.EnhSimGUIFrame;
import org.theqcp.wow.sim.gui.tabs.imagethings.ImagePanel;
import org.theqcp.wow.sim.gui.tabs.imagethings.TalentButton;

public class TalentsPanel extends DataPanel implements ImageObserver, MouseListener {

    private static final long serialVersionUID = 5965930860281144765L;

    public static enum Talent {
	Convection("spell_nature_wispsplode.jpg", 5), Concussion("spell_fire_fireball.jpg", 5),
	CallOfFlame("spell_fire_immolation.jpg", 3), ElementalWarding("spell_nature_spiritarmor.jpg", 3),
	ElementalDevastation("spell_fire_elementaldevastation.jpg", 3), Reverberation("spell_frost_frostward.jpg", 5),
	ElementalFocus("spell_shadow_manaburn.jpg", 1), ElementalFury("spell_fire_volcano.jpg", 5),
	ImprovedFireNovaTotem("spell_fire_sealoffire.jpg", 2), EyeOfTheStorm("spell_shadow_soulleech_2.jpg", 3),
	ElementalReach("spell_nature_stormreach.jpg", 2), CallOfThunder("spell_nature_callstorm.jpg", 1),
	UnrelentingStorm("spell_nature_unrelentingstorm.jpg", 5),
	ElementalPrescision("spell_nature_elementalprecision_1.jpg", 3),
	LightningMastery("spell_lightning_lightningbolt01.jpg", 5), ElementalMastery("spell_nature_wispheal.jpg", 1),
	ElementalShields("spell_nature_elementalshields.jpg", 3), ElementalOath("spell_shaman_elementaloath.jpg", 2),
	LightningOverload("spell_nature_lightningoverload.jpg", 5), AstralShift("spell_shaman_astralshift.jpg", 3),
	TotemOfWrath("spell_fire_totemofwrath.jpg", 1), LavaFlows("spell_shaman_lavaflow.jpg", 3),
	StormEarthAndFire("spell_shaman_stormearthfire.jpg", 5), Thunderstorm("spell_shaman_thunderstorm.jpg", 1);

	private final String iconPath;
	private final int maxRank;

	private Talent(String path, int maxRank) {
	    iconPath = "res/icons/" + path;
	    this.maxRank = maxRank;
	}

	public Image getImage( int width, int height, int hints ) {
	    URL url = this.getClass().getResource("/" + iconPath);

	    if( url != null ) {
		return new ImageIcon(url).getImage().getScaledInstance(width, height, hints);
	    } else {
		return new ImageIcon(iconPath).getImage().getScaledInstance(width, height, hints);
	    }
	}

	public int getMaxRank() {
	    return this.maxRank;
	}
    }

    protected final Talent[][] talents_elemental = { { null, Talent.Convection, Talent.Concussion, null },
	    { Talent.CallOfFlame, Talent.ElementalWarding, Talent.ElementalDevastation, null },
	    { Talent.Reverberation, Talent.ElementalFocus, Talent.ElementalFury, null },
	    { Talent.ImprovedFireNovaTotem, null, null, Talent.EyeOfTheStorm },
	    { Talent.ElementalReach, Talent.CallOfThunder, null, Talent.UnrelentingStorm },
	    { Talent.ElementalPrescision, null, Talent.LightningMastery, null },
	    { null, Talent.ElementalMastery, Talent.ElementalShields, null },
	    { null, Talent.ElementalOath, Talent.LightningOverload, null },
	    { Talent.AstralShift, Talent.TotemOfWrath, Talent.LavaFlows, null },
	    { null, Talent.StormEarthAndFire, null, null }, { null, Talent.Thunderstorm, null, null } };

    public TalentsPanel(EnhSimGUIFrame esgFrame) {
	setupFrame(esgFrame);
    }

    private void setupFrame( EnhSimGUIFrame esgFrame ) {
	String coverPath = "res/icon_border_large.png";
	Image cover;

	URL url = this.getClass().getResource("/" + coverPath);

	if( url != null ) {
	    cover = Toolkit.getDefaultToolkit().createImage(url);
	} else {
	    cover = Toolkit.getDefaultToolkit().createImage(coverPath);
	}

	// cover = new ImageIcon("res/icon_border_large.png").getImage();

	ImagePanel elemental = new ImagePanel("res/backgrounds/shaman_1.jpg");

	GroupLayout gl = new GroupLayout(elemental);
	gl.setAutoCreateContainerGaps(true);
	gl.setAutoCreateGaps(true);

	SequentialGroup hGroup = gl.createSequentialGroup();
	SequentialGroup vGroup = gl.createSequentialGroup();

	ParallelGroup[] hg = new ParallelGroup[] { gl.createParallelGroup(), gl.createParallelGroup(),
		gl.createParallelGroup(), gl.createParallelGroup() };
	ParallelGroup vg;
	for( Talent[] row: talents_elemental ) {
	    vg = gl.createParallelGroup();
	    for( int i = 0; i < row.length; i++ ) {
		Component c;
		if( row[i] == null ) {
		    c = new JPanel().add(new JLabel());
		} else {
		    c = new TalentButton(this, row[i], cover, 44, 44, 8);
		}
		hg[i].addComponent(c);
		vg.addComponent(c);
	    }
	    vGroup.addGroup(vg);
	}

	hGroup.addGroup(hg[0]).addGroup(hg[1]).addGroup(hg[2]).addGroup(hg[3]);

	gl.setHorizontalGroup(hGroup);
	gl.setVerticalGroup(vGroup);

	elemental.setLayout(gl);

	add(elemental);
    }

    @Override
    public void mouseClicked( MouseEvent e ) {
	Component c = e.getComponent();

	if( c instanceof TalentButton ) {
	    TalentButton b = (TalentButton) c;

	    if( e.getButton() == MouseEvent.BUTTON1 ) {
		if( e.isShiftDown() ) {
		    b.setRank(b.getMaxRank());
		} else {
		    b.incRank();
		}
	    } else if( e.getButton() == MouseEvent.BUTTON3 ) {
		if( e.isShiftDown() ) {
		    b.setRank(0);
		} else {
		    b.decRank();
		}
	    }
	}
    }

    @Override
    public void mouseEntered( MouseEvent e ) {
	// System.out.println(e);
    }

    @Override
    public void mouseExited( MouseEvent e ) {
	// System.out.println(e);
    }

    @Override
    public void mousePressed( MouseEvent e ) {
	// System.out.println(e);
    }

    @Override
    public void mouseReleased( MouseEvent e ) {
	// System.out.println(e);
    }

}
